<template>
  <div style="font-size: 14px;background-color: #f5f5f5;">
    <div>
      <div class="addr-box">
        <div v-if="addressInfo.id" class="left">
          <div>
            <span class="name">{{addressInfo.name}}</span>
            <span class="phone">{{addressInfo.tel}}</span>
          </div>
          <div class="addr">
           {{addressInfo.address}}
          </div>
        </div>
        <div v-else class="left tip" @click="toAddress">
          还没有收货地址，请先添加~
        </div>
        <div class="right" @click="toAddress">
          <img src="../../assets/img/in.png">
        </div>
      </div>

      <div class="item-box">
        <div class="item" v-for="(item,index) in products" :key="index">
          <div class="cover">
            <van-image :src="require('../../assets/img/'+item.img)"/>
          </div>
          <div class="product-name">
            {{item.productName}}
          </div>
          <div class="right">
            <div class="price">
              {{item.price.toFixed(2)}}
            </div>
            <div class="num">
              x{{item.num}}
            </div>
          </div>
        </div>
      </div>

      <div class="other">
        <div class="item">
          <div class="title">
            运费
          </div>
          <div class="med"></div>
          <div class="right">
            <div class="des">
              包邮
            </div>
          </div>
        </div>
        <div class="item">
          <div class="title">
            电子普通发票
          </div>
          <div class="med"></div>
          <div class="right">
            <div class="des">
              个人
            </div>
            <div class="more">
              <img src="../../assets/img/in.png">
            </div>
          </div>
        </div>
        <div class="item">
          <div class="title">
            优惠券
          </div>
          <div class="med"></div>
          <div class="right">
            <div class="des">
              无可用
            </div>
            <div class="more">
              <img src="../../assets/img/in.png">
            </div>
          </div>
        </div>
      </div>

      <div class="total-box">
        <div class="item">
          <span class="title">
            商品价格：
          </span>
          <span class="price">
            {{(totalPrice/100).toFixed(2)}}
          </span>
        </div>
        <div class="item">
          <span class="title">
            配送费用：
          </span>
          <span class="price">
            0.00
          </span>
        </div>
      </div>

    </div>

    <div class="but-box">
      <van-submit-bar :price="totalPrice" button-text="提交订单" @submit="onSubmit">
        <span> 共{{totalNum}}件</span>
        <template #tip>
          已享最大优惠，请及时下单
        </template>
      </van-submit-bar>
    </div>

  </div>
</template>

<script>
  import {getStore,setStore,removeStore} from "@/store/store.js";
  import {
    SubmitBar,
    Image as VanImage,
  } from 'vant';
  import data from "@/data/data.json";
  export default {
    components: {
      [SubmitBar.name]: SubmitBar,
      [VanImage.name]: VanImage,
    },
    data() {
      return {
        tabIndex: 1,
        products: [],
        totalNum: 0,
        totalPrice: 0,
        addressInfo:{}
      };
    },
    mounted() {
      this.$store.commit('SET_HEADER_FOOTER', {showSearchHeader: true,headerName:'用户结算',pageName:'pay',showFooter: false});
      let value = this.$route.query.value;
      if(value){
        let arr = value.split('*');
        arr.forEach((item) => {
          let paramArr = item.split('_');
          data.products.forEach((product) => {
            if(product.id == paramArr[0]){
              product['num'] = paramArr[1];
              this.totalNum += parseInt(paramArr[1]);
              this.totalPrice += parseInt(paramArr[1]) * product.price * 100;
              this.products.push(product);
            }
          })
        });
      }
      this.getAddressInfo();
    },
    methods: {
      toAddress(){//收货地址
        if(this.addressInfo.id){
          this.$router.push({
            path: '/address',
            query: {
              id: this.addressInfo.id
            }
          });
        }else{
          this.$router.push({
            path: '/address'
          });
        }

      },
      //格式化日期
      formatDate(date) {
        const dt = new Date(date);
        const y = dt.getFullYear();
        const m = (dt.getMonth() + 1 + '').padStart(2, '0');
        const d = (dt.getDate() + '').padStart(2, '0');

        const hh = (dt.getHours() + '').padStart(2, '0');
        const mm = (dt.getMinutes() + '').padStart(2, '0');
        const ss = (dt.getSeconds() + '').padStart(2, '0');
        // yyyy-mm-dd hh:mm:ss
        return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
      },
      //提交订单
      onSubmit(){
        if(!this.addressInfo.id){
          this.$toast.fail('请选择收货地址');
          return;
        }
        let createTime = this.formatDate(new Date());
        let order = {
          createTime: createTime,
          products: this.products,
          addressInfo: this.addressInfo
        };
        let orderList = getStore({name:'order_list'});
        if(!orderList){
          orderList = [];
        }
        orderList.unshift(order);
        setStore({name:'order_list',content: orderList});
        this.$toast.success('提交成功');
        this.$router.go(-1);
      },
      //获取地址
      getAddressInfo(){
        let obj = getStore({name:'address_pay'});
        if(obj){//重新选择了地址
          this.addressInfo = obj;
          removeStore({name:'address_pay'});
        }else{
          let addressList = getStore({name:'address_list'});
          if (addressList && addressList.length > 0) {
            let defaultAddr = null;
            addressList.forEach((item,index)=>{
              if(item.isDefault){
                defaultAddr = item;
              }
            })
            if(!defaultAddr){
              this.addressInfo = addressList[0];
            }else{
              this.addressInfo = defaultAddr;
            }
          }
        }
        this.addressInfo['address'] = this.addressInfo.province+this.addressInfo.city+this.addressInfo.county+this.addressInfo.addressDetail;
      }
    }
  };
</script>

<style scoped>
  .addr-box{
    height: 80px;
    display: flex;
    padding: 0 10px 0 20px;
    background-color: #FFFFFF;
  }
  .addr-box .left{
    flex: 1;
  }
  .addr-box .right{
    width: 30px;
  }
  .addr-box .right img{
    width: 30px;
    height: 30px;
    margin-top: 25px;
  }
  .addr-box .left .name{
    font-size: 16px;
    font-weight: 600;
    line-height: 40px;
  }
  .addr-box .left .phone{
    margin-left: 10px;
    line-height: 40px;
  }
  .addr-box .left .addr{
    color: #666;
    font-size: 12px;
  }

  .item-box{
    margin-top: 20px;
    background-color: #FFFFFF;
  }
  .item-box .item{
    height: 80px;
    display: flex;
    padding: 10px 10px 10px 20px;
    border-bottom: 1px solid #eee;
  }
  .item-box .item .cover{
    width: 80px;
    height: 80px;
  }
  .item-box .item .cover img{
    width: 80px;
    height: 80px;
  }
  .item-box .item .product-name{
    flex: 1;
    line-height: 80px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    line-clamp: 1;
    -webkit-box-orient: vertical;
    margin-left: 10px;
  }
  .item-box .item .right{
    font-size: 12px;
    min-width: 70px;
    line-height: 40px;
  }
  .item-box .item .right .price{
    font-weight: 600;
    font-size: 16px;
    color:red;
  }
  .item-box .item .right .num{
    color: #999;
    font-size: 14px;
  }

  .other{
    padding: 0 10px 0 20px;
    background-color: #FFFFFF;
    margin-top: 20px;
  }
  .other .item{
    height: 50px;
    line-height: 50px;
    border-bottom: 1px solid #eee;
    display: flex;
  }
  .other .item .title{
    font-weight: 600;
  }
  .other .item .med{
    flex: 1;
  }
  .other .item .right{
    display: flex;
  }
  .other .item .right .des{
    color: #999;
  }
  .other .item .right .more{
    width: 20px;
    margin-left: 5px;
  }
  .other .item .right .more img{
    width: 20px;
    height: 20px;
    margin-top: 15px;
    transform:rotate(90deg);
  }

  .total-box{
    padding: 20px 10px 20px 20px;
    background-color: #FFFFFF;
    margin-top: 20px;
  }
  .total-box .item{
    height: 30px;
    line-height: 30px;
  }
  .total-box .item .title{
    font-weight: 500;
    font-size: 16px;
  }
  .total-box .item .price{
    font-size: 16px;
    color: red;
  }

  .but-box{
    height: 50px;
    line-height: 50px;
    position: absolute;
    bottom: 0;
    display: flex;
    background-color: #FFFFFF;
    width: 100%;
  }
  .but-box .left{
    color: #ff4d14;
    flex: 1;
    margin-left: 20px;
    font-size: 16px;
  }
  .but-box .left .price{
    font-weight: 600;
  }
  .but-box .right{
    width: 200px;
    background-color: #ff5722;
    color: #FFFFFF;
    text-align: center;
  }
  .but-box .right:hover{
    opacity: 0.8;
  }
  .tip{
    line-height: 80px;
  }
</style>
